;(function(){

'use strict';

angular.module('controllers.userManager.roleGrant')
  .controller('roleSelectCtrl',roleSelectCtrl);
  function roleSelectCtrl($scope,roleGrant,CoreService,$state,$cookies){
    var year = new Date().getFullYear();
    var month = new Date().getMonth();
    var date = new Date().getDate();
    $scope.shoumun = {num: true};
    //控制等待样式   false 为隐藏   true 为显示
    $scope._loading.show = false;
    $scope.queries = roleGrant.getQueryData();
    $scope.selectDefined = 'roleCode';

    $scope.param = {
      roleCode: '',
      roleName: '',
      creTime:new Date(year,month,date)
    };
    $scope.search = function(){
      $scope.send = {};
      $scope.shoumun.num ? $scope.shoumun.num = false : $scope.shoumun.num = true;
      if(!$scope.shoumun.num){
        angular.copy($scope.param,$scope.send);
        if($scope.selectDefined === 'creTime'){
          $scope.send.creTime = CoreService.dateFormat($scope.send.creTime,'yyyy-MM-dd');
        }else {
          $scope.send.creTime = '';
        }
        roleGrant.roleSelect($scope.send, function(result) {
          if(result.state === '10000' && result.data.length >=1){
            $scope.items = result.data;
            $scope.flag = 'table';
          }else if(result.data.length === 0){
            $scope.shoumun.num = true;
            CoreService.dialog('查询无数据');
          }else {
            $scope.shoumun.num = true;
            CoreService.dialog('查询错误');
          }
          
        });
      }
    };
    $scope.search();
    $scope.setValue = function() {
      $scope.param = {
        roleCode: '',
        roleName: '',
        creTime:new Date(year,month,date)
      };
      if($scope.selectDefined === 'creTime'){
        $scope.param.creTime = new Date(year,month,date);
      }else {
        $scope.param.creTime = '';
      }
    };
    var choose = {
      roleId :'',
      roleName: ''
    };
    $scope.selectRole = function(role){
      choose.roleId = role.roleId;
      choose.roleName = role.roleName;
    };
    var userIds = [];
    var leng = {};
    $scope.next = function(){
      userIds = [];
      leng = {};
      if(choose.roleId){
        roleGrant.roleModulesQuery({roleId:choose.roleId},callback);
      } else {
        CoreService.dialog('请选择需求授权的角色');
      }
    };
    var myModules = {};   
      
    function callback(res){
      angular.forEach(res.data.allModules, function(parent) {
        myModules[parent.moduleId] = [];
        myModules[parent.moduleId].push(parent.moduleId);
      });
      angular.forEach(res.data.roleModules, function(user) {
        leng[user.moduleId] = user.childModule.length;
        if (userIds.indexOf(user.moduleId) === -1 && user.childModule.length !== 0) {
          userIds.push(user.moduleId);
        }
        angular.forEach(user.childModule, function(child) {
          if (userIds.indexOf(child.moduleId) === -1 ) {
            userIds.push(child.moduleId);
          }
          if (myModules[user.moduleId].indexOf(child.moduleId) !== -1) {
            myModules[user.moduleId].push(child.moduleId);
          }
        });
      });
      angular.forEach(res.data.allModules, function(all) {
        //只有当模块下所有的子模块选中后父模块才会被勾选
        if (userIds.indexOf(all.moduleId) !== -1 && leng[all.moduleId] === all.childModule.length) {
          all.checked = true;
        }
        angular.forEach(all.childModule, function(parent) {

          if (userIds.indexOf(parent.moduleId) !== -1) {
            parent.checked = true;
          }
        });
      });
      $scope.allModules = res.data.allModules;
      $scope.flag = 'next';
    }
    $scope.checkedParent = function(parent){
      angular.forEach(parent.childModule, function(subChild) {
        subChild.checked = parent.checked;
        if (parent.checked) {
          if (myModules[parent.moduleId].indexOf(parent.moduleId) === -1) {
            myModules[parent.moduleId].push(parent.moduleId);
          }
          addModule(parent, subChild.moduleId);
        } else {
          if (myModules[parent.moduleId].indexOf(parent.moduleId) !== -1) {
            myModules[parent.moduleId].splice(myModules[parent.moduleId].indexOf(parent.moduleId), 1);
          }
          delModule(parent, subChild.moduleId);
        }
      });
    };
    $scope.checkedChild = function(child,sub){
      child.checked = sub.checked; //将子节点的值赋给父节点，如果为false，则父节点也为false
      if (child.checked) {
        //如果为true 判断父节点里所有子节点是否都为选中状态
        angular.forEach(child.childModule, function(subChild) {
          if (!subChild.checked) {
            child.checked = false; //有一个没选中，则父节点也不能被选中
          }
        });
        //将选中的节点添加到模块中
        addModule(child, sub.moduleId);
      } else {
        //移除选中的节点
        delModule(child, sub.moduleId);
      }
    };
    //删除模块
    function delModule(parent, id) {
      //找到相同就删除
      angular.forEach(myModules[parent.moduleId], function(moduleId, index) {
        if (id === moduleId) {
          myModules[parent.moduleId].splice(index, 1);
        }
      });
    }
    //新增模块
    function addModule(parent, id) {
      var has = false;
      //如果找到相同的ID，就添加到数组中
      angular.forEach(myModules[parent.moduleId], function(moduleId) {
        if (id === moduleId) {
          has = true;
        }
      });
      has || myModules[parent.moduleId].push(id);
    }
    $scope.save = function(){
      var param = {
        moduleIds: [],
        roleId: choose.roleId
      };
      angular.forEach(myModules, function(item) {
        item.map(function(ids) {
          param.moduleIds.push(ids);
        });
      });
      roleGrant.roleBind(param,function(res){
        if(res.state === '10000'){
          alert('保存成功');
          $scope.flag = 'table';
        }
      });
    };
    $scope.reset = function(){
      roleGrant.roleModulesQuery({roleId:choose.roleId},callback);
    };
  }
  roleSelectCtrl.$inject = ['$scope','roleGrant','CoreService','$state','$cookies'];
})();